package com.turing.post.thirdinterface.impl;

import java.io.Serializable;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.turing.post.thirdinterface.util.ThirdInterfaceApplicationException;
import com.turing.post.thirdinterface.util.ThirdInterfaceRespCode;
import com.turing.post.tuxedo.util.ParaseException;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 请求三环基类
 * @author ZXL
 *
 */
@SuppressWarnings("serial")
public class SanHuanResponse implements Serializable {
	/**
	 * 交易码
	 */
	public String tranCode = "";
	/**
	 * 车驾号
	 */
	public String vin = "";

	/**
	 * 车主姓名
	 */
	public String name = "";

	/**
	 * 卡号
	 */
	public String cardNo = "";
	
	/**
	 * 注册编号
	 */
	public String regNo = "";
	
	/**
	 * 原卡号
	 */
	public String oldCardNo = "";
	
	/**
	 * 构造开卡验证XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructOpenCardValidateXml(SanHuanResponse sanHuanResponse) throws PCPSApplicationException {
		
		Element root = new Element(SanHuanXmlHelper.XML_NODE_TRANSREQUEST); 
		SanHuanXmlHelper.addStringElement(root,SanHuanXmlHelper.XML_NODE_TRANCODE,
				sanHuanResponse.tranCode);
		Element reqBody = new Element(SanHuanXmlHelper.XML_NODE_REQBODY);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_VIN,
				sanHuanResponse.vin);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_NAME,
				sanHuanResponse.name);
		root.addContent(reqBody);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				SanHuanXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					SanHuanXmlHelper.XML_CHARSET_GB18030);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	/**
	 * 构造开卡注册XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructOpenCardRegisterXml(SanHuanResponse sanHuanResponse) throws PCPSApplicationException {
		Element root = new Element(SanHuanXmlHelper.XML_NODE_TRANSREQUEST); 
		SanHuanXmlHelper.addStringElement(root,SanHuanXmlHelper.XML_NODE_TRANCODE,
				sanHuanResponse.tranCode);
		Element reqBody = new Element(SanHuanXmlHelper.XML_NODE_REQBODY);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_REGNO,
				sanHuanResponse.regNo);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_CARDNO,
				sanHuanResponse.cardNo);
		root.addContent(reqBody);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				SanHuanXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					SanHuanXmlHelper.XML_CHARSET_GB18030);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	/**
	 * 构造换卡XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructChangeCardXml(SanHuanResponse sanHuanResponse) throws PCPSApplicationException {
		
		Element root = new Element(SanHuanXmlHelper.XML_NODE_TRANSREQUEST); 
		SanHuanXmlHelper.addStringElement(root,SanHuanXmlHelper.XML_NODE_TRANCODE,
				sanHuanResponse.tranCode);
		Element reqBody = new Element(SanHuanXmlHelper.XML_NODE_REQBODY);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_OLDCARDNO,
				sanHuanResponse.oldCardNo);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_CARDNO,
				sanHuanResponse.cardNo);
		root.addContent(reqBody);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				SanHuanXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					SanHuanXmlHelper.XML_CHARSET_GB18030);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	
	/**
	 * 构造黑名单、解除黑名单、卡赎回XML请求报文
	 * 
	 * @param doc
	 */
	public static byte[] constructCurrencyXml(SanHuanResponse sanHuanResponse) throws PCPSApplicationException {
		
		Element root = new Element(SanHuanXmlHelper.XML_NODE_TRANSREQUEST); 
		SanHuanXmlHelper.addStringElement(root,SanHuanXmlHelper.XML_NODE_TRANCODE,
				sanHuanResponse.tranCode);
		Element reqBody = new Element(SanHuanXmlHelper.XML_NODE_REQBODY);
		SanHuanXmlHelper.addStringElement(reqBody,SanHuanXmlHelper.XML_NODE_CARDNO,
				sanHuanResponse.cardNo);
		root.addContent(reqBody);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getCompactFormat().setEncoding(
				SanHuanXmlHelper.XML_CHARSET_UTF));
		try {
			return out.outputString(doc).trim().getBytes(
					SanHuanXmlHelper.XML_CHARSET_GB18030);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
	
	/**
	 * 
	 * @param sanHuanResponse
	 * @return
	 * @throws PCPSApplicationException
	 */
	public static byte[] checkResponseBusinessType(SanHuanResponse sanHuanResponse) throws PCPSApplicationException {
		//验证参数
		if (sanHuanResponse == null){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "参数为空!");
		}
		//验证业务类型
		if (PCPSUtil.isEmpty(sanHuanResponse.tranCode)){
			throw new ThirdInterfaceApplicationException(
					ThirdInterfaceApplicationException.ERRMSG_NULLPOINTER,
					ThirdInterfaceRespCode.RC_ARGS_NULL, "交易码为空!");
		}
		//开卡验证
		if (SanHuanXmlHelper.XML_OPENCARDVALIDATE.equals(sanHuanResponse.tranCode)){
			return constructOpenCardValidateXml(sanHuanResponse);
		}
		//开卡注册
		if (SanHuanXmlHelper.XML_OPENCARDREGISTER.equals(sanHuanResponse.tranCode)){
			return constructOpenCardRegisterXml(sanHuanResponse);
		}
		//换卡
		if (SanHuanXmlHelper.XML_CHANGECARD.equals(sanHuanResponse.tranCode)){
			return constructChangeCardXml(sanHuanResponse);
		}
		//黑名单、解除黑名单、卡赎回
		if (SanHuanXmlHelper.XML_BLACKORDER.equals(sanHuanResponse.tranCode)
				|| SanHuanXmlHelper.XML_REMOVEBLACKORDER.equals(sanHuanResponse.tranCode)
				|| SanHuanXmlHelper.XML_CARDREDEMP.equals(sanHuanResponse.tranCode)){
			return constructCurrencyXml(sanHuanResponse);
		}
		return null;
	}
}
